<script>
import { api } from 'util/api.js';
import QQMapWX from '@/util/qqmap-wx-jssdk.min.js';
let qqmapsdk;

export default {
	globalData: {
		navHeight: 0,
		openid: '',
		member: '',
		regionList: [],
		nowRegion: null,
		loginStatus: 0
	},
	methods: {
		login: function() {
			return new Promise((resolve, reject) => {
				// 缓存登录状态，0为未操作登录，400为用户未登录，200为用户登录成功可通过member获取数据
				if (uni.getStorageSync('loginStatus') === undefined || uni.getStorageSync('loginStatus') === '') {
					uni.setStorageSync('loginStatus', 0);
				}

				uni.removeStorageSync('access_token');
				// // 第一步获取用户小程序信息
				uni.login({
					provider: 'weixin',
					success: res => {
						if (res.errMsg === 'login:ok' && res.code !== undefined && res.code !== '') {
							var param = { code: res.code };
							api.login(param, e => {
								uni.setStorageSync('loginStatus', 400);
								this.globalData.loginStatus = 0;
								if (e.data.openid !== null && e.data.openid !== '') this.globalData.openid = e.data.openid;
								if (e.code === 200) {
									let user = e.data.user;
									this.globalData.member = user;
									let access_token = e.data.token;
									uni.setStorageSync('loginStatus', 200);
									uni.setStorageSync('access_token', access_token);
									this.globalData.loginStatus = 1;
								}
								// 获取区域列表
								api.getRegionList({}, res => {
									if (res.code === 200) {
										let i = res.data.findIndex(item => item.id === this.globalData.member.region_id);
										if (i !== -1) {
											this.globalData.nowRegion = res.data[i];
										}else{
											this.globalData.nowRegion = res.data[0];
										}
										this.globalData.regionList = res.data;
										resolve(e);
									} else {
										uni.showToast({
											title: res.msg,
											icon: 'null'
										});
										reject(res.msg);
									}
								});
							});
						}
					},
					fail: err => {
						reject(err);
					}
				});
			});
		},
		isOpen(id) {
			if (this.globalData.nowRegion !== null) {
				var i = this.globalData.nowRegion.jurisdiction.split(',').findIndex(item => parseInt(item) === id);
				if (i >= 0) {
						console.log('11');
					return true;
				} else {
						console.log('22');
					return false;
				
				}
			} else {
					console.log('33');
				return false;
				
			}
		}
	},
	onLaunch: function(option) {
		qqmapsdk = new QQMapWX({
			key: 'XQGBZ-STJRG-EM4Q5-ICYZH-VMHH2-GBB7J' // 必填
		});

		// 初始化导航栏高度
		uni.getSystemInfo({
			success: res => {
				this.globalData.navHeight = res.statusBarHeight + 37; // 赋值导航高度
				// console.log(this.globalData.navHeight);
				// this.globalData.navHeight = wx.getMenuButtonBoundingClientRect().top + 33;
				// console.log(this.globalData.navHeight);
			},
			fail(err) {
				console.log(err);
			}
		});
		this.login();
	},
	onShow: function() {
		console.log('App Show');
	},
	onHide: function() {
		console.log('App Hide');
	}
};
</script>

<style>
/*每个页面公共css */

page {
	height: 100%;
	width: 100%;
	background-color: #f4f4f4;
}
button::after {
	border: none;
}
.page-wrapper {
	height: 100%;
	width: 100%;
	position: relative;
	overflow: auto;
}

.flex {
	display: flex;
}
.align {
	align-items: center;
}
.between {
	justify-content: space-between;
}
.center {
	justify-content: center;
}
.column{
	flex-direction: column;
}

@font-face {
	font-family: 'PINGFANG-JIAN';
	src: url('https://www.jiajiedaojia.com/fonts/PingFang-Jian.ttf');
}
@font-face {
	font-family: 'DIN-Condensed-Bold';
	src: url('https://www.jiajiedaojia.com/fonts/DIN-Condensed-Bold.ttf');
}
:not(not) {
	font-family: 'PINGFANG-JIAN';
}
/* ::-webkit-scrollbar {
		width: 0;
		height: 0;
		color: transparent;
	} */
::-webkit-scrollbar {
	display: none;
}
</style>
